'use strict';

const Service = require('egg').Service;

class LeetCodeService extends Service {
  async getLeetCodeList() {
    const list = await this.app.mysql
      .query(`SELECT id, url,title,passing_rate,level,
    (SELECT IFNULL(SUM(is_like), 0) FROM leetcode_like WHERE id = leetcode_id) AS like_count,
    (SELECT IFNULL(SUM(is_collect), 0) FROM leetcode_like WHERE id = leetcode_id) AS collect_count
    FROM leetcode`);
    return { list };
  }

  async getLeetCodeInfo(userId) {
    const list = await this.app.mysql
      .query(`SELECT SUM(is_done) AS done_count, SUM(is_like) AS like_count, SUM(is_collect) AS collect_count
      FROM leetcode_like
      WHERE user_id = ${userId}`);
    return { list };
  }

  async getLeetCodeUserInfo(leetcodeId, userId) {
    let data = await this.app.mysql.get('leetcode_like', {
      leetcode_id: leetcodeId,
      user_id: userId,
    });
    if (!data) {
      data = {
        is_like: 0,
        is_collect: 0,
        is_done: 0,
      };
    }
    return { data };
  }

  async liekLeetCode(leetcodeId, userId) {
    const getItem = await this.app.mysql.get('leetcode_like', {
      leetcode_id: leetcodeId,
      user_id: userId,
    });
    let result;
    let insertSuccess = true;
    if (getItem) {
      if (getItem.is_like) return { insertSuccess };
      const data = {
        is_like: 1,
      };
      const options = {
        where: {
          leetcode_id: leetcodeId,
          user_id: userId,
        },
      };
      result = await this.app.mysql.update('leetcode_like', data, options);
    } else {
      result = await this.app.mysql.insert('leetcode_like', {
        leetcode_id: leetcodeId,
        user_id: userId,
        is_like: 1,
        is_collect: 0,
        is_done: 0,
      });
    }
    insertSuccess = result.affectedRows === 1;
    return { insertSuccess };
  }

  async disliekLeetCode(leetcodeId, userId) {
    const data = {
      is_like: 0,
    };
    const options = {
      where: {
        leetcode_id: leetcodeId,
        user_id: userId,
      },
    };
    const result = await this.app.mysql.update('leetcode_like', data, options);
    const updateSuccess = result.affectedRows === 1;
    return { updateSuccess };
  }

  async collectLeetCode(leetcodeId, userId) {
    const getItem = await this.app.mysql.get('leetcode_like', {
      leetcode_id: leetcodeId,
      user_id: userId,
    });
    let result;
    let insertSuccess = true;
    if (getItem) {
      if (getItem.is_collect) return { insertSuccess };
      const data = {
        is_collect: 1,
      };
      const options = {
        where: {
          leetcode_id: leetcodeId,
          user_id: userId,
        },
      };
      result = await this.app.mysql.update('leetcode_like', data, options);
    } else {
      result = await this.app.mysql.insert('leetcode_like', {
        leetcode_id: leetcodeId,
        user_id: userId,
        is_like: 0,
        is_collect: 1,
        is_done: 0,
      });
    }
    insertSuccess = result.affectedRows === 1;
    return { insertSuccess };
  }

  async cancelCollectLeetCode(leetcodeId, userId) {
    const data = {
      is_collect: 0,
    };
    const options = {
      where: {
        leetcode_id: leetcodeId,
        user_id: userId,
      },
    };
    const result = await this.app.mysql.update('leetcode_like', data, options);
    const updateSuccess = result.affectedRows === 1;
    return { updateSuccess };
  }

  async doneLeetCode(leetcodeId, userId) {
    const getItem = await this.app.mysql.get('leetcode_like', {
      leetcode_id: leetcodeId,
      user_id: userId,
    });
    let result;
    let insertSuccess = true;
    if (getItem) {
      if (getItem.is_done) return { insertSuccess };
      const data = {
        is_done: 1,
      };
      const options = {
        where: {
          leetcode_id: leetcodeId,
          user_id: userId,
        },
      };
      result = await this.app.mysql.update('leetcode_like', data, options);
    } else {
      result = await this.app.mysql.insert('leetcode_like', {
        leetcode_id: leetcodeId,
        user_id: userId,
        is_like: 0,
        is_collect: 0,
        is_done: 1,
      });
    }
    insertSuccess = result.affectedRows === 1;
    return { insertSuccess };
  }

  async cancelDoLeetCode(leetcodeId, userId) {
    const data = {
      is_done: 0,
    };
    const options = {
      where: {
        leetcode_id: leetcodeId,
        user_id: userId,
      },
    };
    const result = await this.app.mysql.update('leetcode_like', data, options);
    const updateSuccess = result.affectedRows === 1;
    return { updateSuccess };
  }
}

module.exports = LeetCodeService;
